/**
 *
 * @param urls     获取的图片地址数组
 * @param handler  获取图片的方法
 * @param limit    每次固定获取图片的数量， 当有一个完成， 就塞进一个新的
 *
 */
function limitLoad(urls, handler, limit) {
    const queues = urls
    let promises = []
    promises = queues.splice(0,limit).map((url, index) => {
        return handler(url).then(() => {
            return index
        })
    })
    let p = Promise.race(promises)
    for(let i=0; i<promises.length; i++) {
        p = p.then((res) =>{
            promises[res] = handler(queues[i]).then(() => {
                return res;
            })
            return Promise.race([promises])
        })
    }
}

const urls = [
    {
        info:'url1',
        limit:100
    },{
        info:'url2',
        limit:200
    },{
        info:'url3',
        limit:100
    },{
        info:'url4',
        limit:300
    },{
        info:'url5',
        limit:500
    },{
        info:'url6',
        limit:600
    },{
        info:'url7',
        limit:200
    },
]

function loadImg(url) {
    return new Promise(((resolve, reject) => {
        console.log('开始执行'+url.info)
        // 模拟异步网络请求
        setTimeout(() => {
            console.log('执行完成'+url.info)
            resolve()
        }, url.limit)
    }))
}


limitLoad(urls, loadImg, 3)
